package org.nmgyjt.springboot.utils;

import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class ServerIpUtils {

    public static String getServerIpAddress() {
        try {
            // 获取本地主机
            InetAddress localhost = InetAddress.getLocalHost();
            // 返回主机地址
            return localhost.getHostAddress();
        } catch (UnknownHostException e) {
            throw new RuntimeException("无法获取服务器IP地址", e);
        }
    }


    public static void main(String[] args) {
        String serverIp = ServerIpUtils.getServerIpAddress();
        System.out.println("服务器IP地址: " + serverIp);
    }
}
